const {db} = require("../util/util")

const addBoard = (name) => {//添加候选人
  sql = "insert into board (name) values(?)";
  return db.execute(sql,[name]).then((data) => {
    return data[0].insertId;// 判断是否插入成功 并返回id
  })
}

const getBoardListByEventId = (event_id) => {//通过投票事件id获取该事件的候选人列表
  sql = "select * from board where id in(select board_id from event_board where event_id = ?)"
  return db.execute(sql,[event_id]).then((data) => {
    return data[0];//获取board列表
  })
}

const updateTotalAmount = (id) => {//候选人票数加一
  sql = "update board set total_amount = total_amount+1 where id = ?";
  return db.execute(sql,[id]).then((data) => {
    return data[0].affectedRows;//判断是否更新成功
  })
}


module.exports = {addBoard, getBoardListByEventId, updateTotalAmount}

// updateTotalAmount(5).then((data) => {
//   console.log(data);
// })